package com.javaear.zblog4java.system.controller.manager;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.javaear.generalmapper.GeneralMapper;
import com.javaear.zblog4java.system.component.AppConfig;
import com.javaear.zblog4java.system.entity.dto.ArticleDTO;
import com.javaear.zblog4java.system.entity.model.ArticleModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

/**
 * @author aooer
 */
@Controller
@RequestMapping("system/management")
public class ArticleManagerController {

    @Autowired
    private GeneralMapper generalMapper;

    @RequestMapping("article-management")
    public void list(HttpServletRequest request, ModelMap modelMap) {
        int pageNo = 1;
        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isNotBlank(pageNoStr) && StringUtils.isNumeric(pageNoStr))
            pageNo = Integer.parseInt(pageNoStr);
        if (pageNo < 1)
            pageNo = 1;
        ArticleModel serachModel = new ArticleModel();
        String searchCateId = request.getParameter("category");
        if (StringUtils.isNotBlank(searchCateId) && StringUtils.isNumeric(searchCateId))
            serachModel.setCateId(Integer.parseInt(searchCateId));
        String searchStatus = request.getParameter("status");
        if (StringUtils.isNotBlank(searchStatus) && StringUtils.isNumeric(searchStatus))
            serachModel.setStatus(Integer.parseInt(searchStatus));
        String searchTitle = request.getParameter("title");
        if (StringUtils.isNotBlank(searchTitle) && StringUtils.isNumeric(searchTitle))
            serachModel.setTitle(searchTitle);
        Page<ArticleModel> page = new Page<>(pageNo, (int) AppConfig.settingMap.get("App-systemPageSize"));
        modelMap.put("articles", generalMapper.selectPage(page, new EntityWrapper<>(serachModel)).stream().map(ArticleDTO::new).collect(Collectors.toList()));
        modelMap.put("page", page);
        modelMap.put("title", "文章管理");
    }

    /*@RequestMapping("article-add")
    public String add() {
        return managerService.add();
    }

    @RequestMapping("article-edit")
    public String edit() {
        return managerService.edit();
    }

    @RequestMapping("article-remove")
    public String remove() {
        return managerService.remove();
    }*/

}
